/*
 * @Author: 晟松
 * @Date: 2021-12-11 15:12:56
 * @LastEditors: 晟松
 * @LastEditTime: 2022-02-26 21:08:52
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\controller\GroupVolController.js
 * @Description: 大型志愿服务的一些操作
 */

import Router from "koa-router";
import koaBodyparser from "koa-bodyparser";
import GroupVolService from "@main/service/GroupVolService ";
import allowUserIdentity from "@main/middleware/routeMid/validateIdentity";
import { LoginError } from "@main/common/CommonError";

let groupVolRouter = new Router({
    prefix: "/api/groupVol",
});
/**
 * @description: 居委会发起大型志愿服务
 * @param {*}
 * @return {*}
 */
groupVolRouter.post("/add", koaBodyparser(), allowUserIdentity("community"), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.add(params));
});
groupVolRouter.post("/delete", koaBodyparser(), allowUserIdentity("community"), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.delete(params));
});

groupVolRouter.post("/update", koaBodyparser(), allowUserIdentity(["school", "community"]), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.update(params));
});
/**
 * @description: 获得一个大型志愿服务的信息
 * @param {*}
 * @return {*}
 */
groupVolRouter.post("/getGroupVolInfo", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.getGroupVolInfo(params));
});
/**
 * @description: 获得一个大型志愿服务的所有关联的学生，即领取了这个大型志愿服务的学生
 * @param {*}
 * @return {*}
 */
groupVolRouter.post("/getGroupVolRelativeStudent", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.getGroupVolRelativeStudent(params));
});

/**
 * @description: 居委会获得自己所有的大型志愿服务记录
 * @param {*} params
 * @return {*}
 */
groupVolRouter.post("/getMineGroupVol", koaBodyparser(), allowUserIdentity("community"), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.getMineGroupVol(params));
});
/**
 * @description: 学校获得自己的大型志愿服务
 * @param {*} params
 * @return {*}
 */
groupVolRouter.post("/schoolGetMineGroupVol", koaBodyparser(), allowUserIdentity(["school", "community", "student"]), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.schoolGetMineGroupVol(params));
});

/**
 * @description: 学生获取自己领取的大型志愿服务
 * @param {*}
 * @return {*}
 */
groupVolRouter.post(
    "/studentGetMineGroupVol",
    koaBodyparser(),
    // allowUserIdentity("community"),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        // ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(await GroupVolService.studentGetMineGroupVol(params));
    }
);
/**
 * @description: 学生获取自己学校或学院领取 / 居委会公开的大型志愿服务
 * @param {*}
 * @return {*}
 */
groupVolRouter.post(
    "/studentGetGroupVolList",
    koaBodyparser(),
    // allowUserIdentity("community"),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        // ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(await GroupVolService.studentGetGroupVolList(params));
    }
);
/**
 * @description: 居民获取自己居委会发起的大型志愿服务
 * @param {*}
 * @return {*}
 */
groupVolRouter.post(
    "/residentGetGroupVolList",
    koaBodyparser(),
    // allowUserIdentity("community"),
    async (ctx) => {
        let params = ctx.request.body;
        let user = ctx.state.user;
        console.log("params: ", params, user);
        // user:
        /* {
            id: 37,
            phone: '13206070001',
            identity: 'resident',
            createdAt: '2021-12-13 17:14:12',
            updatedAt: '2021-12-13 17:14:12',
            student: null,
            resident: {
              id: 3,
              identity: 16,
              userName: '居民2',
              avatar: 'http://qiniu.chengsong.info/img/1639368434603.jpg',
              gender: 1,
              age: 55,
              specialDescription: null,
              communityName: '天马社区',
              detailedAddress: null,
              available: true,
              timeIntegral: 100,
              createdAt: '2021-12-13 17:14:12',
              updatedAt: '2021-12-13 17:14:12',
              loginId: 37,
              communityId: 1
            },
            school: null,
            community: null,
            iat: 1645763352,
            exp: 1646627352
          } */
        // ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(await GroupVolService.residentGetGroupVolList(params, user.resident));
    }
);

/**
 * @description: 获得公开的大型志愿服务
 * @param {*}
 * @return {*}
 */
groupVolRouter.post(
    "/getPublicGroupVol",
    koaBodyparser(),
    // allowUserIdentity("community"),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        // ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(await GroupVolService.getPublicGroupVol(params));
    }
);

/**
 * @description: 学校或院级辅导员领取
 * @param {*}
 * @return {*}
 */
groupVolRouter.post("/receiveGroupVol", allowUserIdentity(["school", "community"]), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.receiveGroupVol(params));
});
/**
 * @description: 院级辅导员或校级管理员指定学生完成自己领取的大型志愿服务或学生自行领取
 * @param {*}
 * @return {*}
 */
groupVolRouter.post("/studentReceiveGroupVol", allowUserIdentity(["school", "student"]), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.studentReceiveGroupVol(params));
});

/**
 * @description: 学生获取自己可领取的大型志愿服务列表
 * @param {*}
 * @return {*}
 */
//  groupVolRouter.post(
//     "/studentGetGroupVolList",
//     allowUserIdentity(["school", "student"]),
//     koaBodyparser(),
//     async (ctx) => {
//         // let params = ctx.request.body;
//         // 获取token对应的用户信息，即学生信息
//         let params = ctx.state.user
//         console.log("params: ", params);
//         // ctx.validateParams.validateParamId(params);

//         ctx.body = ctx.renderJson(
//             await GroupVolService.studentGetGroupVolList(params)
//         );
//     }
// );

/**
 * @description: 获得一个大型志愿服务相关连的所有学生
 * @param {*}
 * @return {*}
 */
groupVolRouter.post(
    "/getRelativeStudent",
    // allowUserIdentity(["school", "student"]),
    koaBodyparser(),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        // ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(await GroupVolService.getRelativeStudent(params));
    }
);
/**
 * @description: 校级管理员或院级辅导员获得自己的学生的大型志愿服务
 * @param {*}
 * @return {*}
 */
groupVolRouter.post("/schoolGetStudentGroupVol", allowUserIdentity(["school", "community"]), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    let user = ctx.state.user;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.schoolGetStudentGroupVol(params, user));
});
/**
 * @description: 接受者即学生完成请求发起反馈
 * @param {*}
 * @return {*}
 */
groupVolRouter.post(
    "/feedbackGroupVol",
    // allowUserIdentity(["school", "community"]),
    koaBodyparser(),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        // ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(await GroupVolService.feedbackGroupVol(params));
    }
);

/**
 * @description: 发起者评价一个志愿服务
 * @param {*}
 * @return {*}
 */
groupVolRouter.post("/evaluateGroupVol", allowUserIdentity(["school", "community"]), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await GroupVolService.evaluateGroupVol(params));
});

export default groupVolRouter;
